<template>
  <div class='container'>
    <el-dialog :title="title" width="25%" :visible="$parent.showdialog" @close="handleClose">
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="学科名称" prop='subjectName'>
          <el-input v-model="form.subjectName" placeholder="请输入学科名称" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
          v-model="form.isFrontDisplay"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="submit">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '../../api/hmmm/subjects'
export default {

  data () {
    return {
      form: {
        subjectName: '',
        isFrontDisplay: 0
      },
      rules: {
        subjectName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        isFrontDisplay: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title () {
      return this.form.creatorID ? '修改学科' : '新增学科'
    }
  },
  methods: {
    handleClose () {
      this.$parent.showdialog = false
      this.$refs.form.resetFields()
      this.form = {
        subjectName: '',
        isFrontDisplay: 0
      }
    },
    async submit () {
      await this.$refs.form.validate()
      this.form.creatorID ? await update({ id: this.form.id, subjectName: this.form.subjectName, isFrontDisplay: this.form.isFrontDisplay }) : await add(this.form)
      this.$message.success(`${this.form.creatorID ? '修改' : '新增'}学科成功`)
      this.handleClose()
      this.$parent.getList()
    },
    async edit (id) {
      const res = await detail({ id })
      this.form = res.data
    }
  }
}
</script>

<style scoped lang='less'></style>
